<?php

namespace App\Datas;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use mysql_xdevapi\Table;

class Upgrade
{

    const UPGRADE_KEY = "UPGRADE:CHANNEL"; //hash UPGRADE:CHANNEL

    /**
     * @desc 通过渠道号获取最新升级信息
     * @param $channelId 渠道号
     */ 
    public static function getChannelData($channelId) 
    {
        $redisData = Redis::hget(self::UPGRADE_KEY, $channelId);
        if ($redisData) {
            return json_decode($redisData);
        }

        $channelData = DB::table('upgrade')->where(['channel_id' => $channelId])->first();
        if(!$channelData) {
            return null;
        }
        $inrData = json_encode($channelData);
        Redis::hset(self::UPGRADE_KEY, $channelId, $inrData);
        return $channelData;
    }

}